//
// Created by shanshi on 18/6/2025.
//
// 智能指针是：智能指针，自动管理对象，自动释放对象。类似于析构函数自动释放
#include <iostream>
#include <memory>

// case1
# if 0
template<class X>
class autpptr {
public:
    explicit autpptr(X *p = nullptr) : p(p) {}
private:
    X *p;
};

class report {
public:
    report() {
        std::cout << "constructor" << std::endl;
    }
    void commit(std::string s) {
        std::cout<< "you commit" << s << std::endl;
    }
    ~report() {
        std::cout << "destructor" << std::endl;
    }
};
int main() {
    // using namespace std;
    // auto_ptr<double> p(new double(3.14));
    // cout << "p:" <<p.get() <<endl;
    // cout << "*p:" <<*p <<endl;
    // cout << "p:" <<p.operator*()<<endl;
    // cout << "p:" <<p.operator->()<<endl;
    {
        std::auto_ptr<report> p(new report());
        p->commit("111hello world");
    }
    {
        std::unique_ptr<report> p(new report());
        p->commit("222hello world");
    }
}
#endif
// case2
#if 0
int main() {
    using namespace std;
    std::auto_ptr<std::string> strs[5]= {
        auto_ptr<std::string> (new std::string("hello")),
        auto_ptr<std::string> (new std::string("world")),
        auto_ptr<std::string> (new std::string("shanshi")),
        auto_ptr<std::string> (new std::string("c++")),
        auto_ptr<std::string> (new std::string("c"))
    };
    auto_ptr<std::string> pwin;
    pwin = strs[2];
    // 转移回ownership所有权
    strs[2] = pwin;

    for (int i = 0; i < 5; ++i) {
        cout << *strs[i] << endl;
    }
}
#endif
// case3
#if 0
int main() {
    using namespace std;
    std::shared_ptr<std::string> strs[5]= {
        shared_ptr<std::string> (new std::string("hello")),
        shared_ptr<std::string> (new std::string("world")),
        shared_ptr<std::string> (new std::string("shanshi")),
        shared_ptr<std::string> (new std::string("c++")),
        shared_ptr<std::string> (new std::string("c"))
    };
    shared_ptr<std::string> pwin;
    pwin = strs[2];
    // 转移回ownership所有权

    for (int i = 0; i < 5; ++i) {
        cout << *strs[i] << endl;
    }
}
#endif
// case4
#if 0
int main() {
    using namespace std;
    std::unique_ptr<std::string> strs[5]= {
        unique_ptr<std::string> (new std::string("hello")),
        unique_ptr<std::string> (new std::string("world")),
        unique_ptr<std::string> (new std::string("shanshi")),
        unique_ptr<std::string> (new std::string("c++")),
        unique_ptr<std::string> (new std::string("c"))
    };
    unique_ptr<std::string> pwin;
    // 转移回ownership所有权
    for (int i = 0; i < 5; ++i) {
        cout << *strs[i] << endl;
    }
}
#endif
// case5
#if 1

#endif